<?php
include_once(dirname(__FILE__).'/../autoload.php');

class Acceptance_Controller_userTest extends Lib_AcceptanceTestcase
{
    public function setUp()
    {
        ob_start();
    }
    public function teardown()
    {
        ob_end_clean();
    }

    public function test_add()
    {
        $inputUser = array(
            'username' => 'Bond',
            'email' => 'james.bond@gmail.com',
            'userGroupId' => 1,
            'userStatusId' => 1,
            'password' => 'Licence2K1ll',
            'password2' => 'Licence2K1ll'
        );
        $expectedUser = array(
            'id' => 6,
            'username' => 'Bond',
            'email' => 'james.bond@gmail.com',
            'userGroupId' => 1,
            'userStatusId' => 1,
            'password' => '',
            'password2' => ''
        );
        $controller = self::initController('User', 
                array('data' => json_encode($inputUser)));
        $controller->_add();
        unset($controller);

        $expected = '{"success":true,"message":"User Bond added","data":['.
            json_encode($expectedUser).'],"total":1}';
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual, 'Unexpected output');

        $expectedPW = md5('BondLicence2K1ll'.Model_User::PASS_SALT);
        $actualPW = Lib_QueryWrapper::runQuery('SELECT password FROM User WHERE id = 6');
        $actualPW = $actualPW[0]['password'];
        $this->assertEquals($expectedPW, 
                $actualPW, 'DB row was not expected');
    }

    public function test_delete()
    {
        $expected = '{"success":true,"message":"User removed"}';
        $controller = self::initController('User', array('data' => 5));
        $controller->_delete();
        unset($controller);
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual, 'Invalid message to UI');

        //Check that statuscode is updated
        $statusCode = Lib_QueryWrapper::runQuery(
                'SELECT userStatusId FROM User WHERE id = 5');
        $statusCode = $statusCode[0]['userStatusId'];
        $this->assertEquals(Model_User::USER_STATUS_REMOVED, $statusCode);

    }

    public function test_getAssignees()
    {
        Lib_QueryWrapper::initializeTestData();
        $expectedList = json_encode(array(
            'success' => true,
            'message' => null,
            'data' => array(
                array('userId' => '1', 'username' => 'Dude'),
                array('userId' => '3', 'username' => 'Jack'),
                array('userId' => '4', 'username' => 'Mulder')
            ),
            'total' => 3
        ));

        $controller = self::initController('User');
        $controller->_getAssignees();
        unset($controller);
        $actualList = self::getOutput();
        $this->assertEquals($expectedList,  $actualList);
    }

    public function test_getGroups()
    {
        $controller = self::initController('User');
        $controller->_getGroups();
        unset($controller);

        $expected = '{"success":true,"message":null,"data":[{"userGroupId":"1","text":"Admin"},{"userGroupId":"2","text":"User"},{"userGroupId":"3","text":"Observer"}],"total":3}';
        $actual = self::getOutput();

        $this->assertEquals($expected, $actual, 'Unexpected output');
    }

    public function test_getStatuses()
    {
        $controller = self::initController('User');
        $controller->_getStatuses();
        unset($controller);

        $expected = '{"success":true,"message":null,"data":[{"userStatusId":"1","text":"Active"},{"userStatusId":"2","text":"Passive"},{"userStatusId":"3","text":"Removed"}],"total":3}';
        $actual = self::getOutput();

        $this->assertEquals($expected, $actual, 'Unexpected output');
    }

    public function test_load()
    {
        Lib_QueryWrapper::initializeTestData();
        $expectedList = json_encode(array(
            'success' => true,
            'message' => null,
            'data' => array(
                array('id' => '1', 'username' => 'Dude', 'email' => null, 'userGroupId' => '1', 'userStatusId' => '1'),
                array('id' => '3', 'username' => 'Jack', 'email' => null, 'userGroupId' => '3', 'userStatusId' => '1'),
                array('id' => '2', 'username' => 'JustAGuy', 'email' => null, 'userGroupId' => '2', 'userStatusId' => '2'),
                array('id' => '4', 'username' => 'Mulder', 'email' => null, 'userGroupId' => '1', 'userStatusId' => '1')
             ),
            'total' => 4
        ));
        $controller = self::initController('User');
        $controller->_load();
        unset($controller);
        $actualList = self::getOutput();
        $this->assertEquals($expectedList, $actualList);
    }

    public function test_update()
    {
        $inputUser = array(
            'id' => 2,
            'username' => 'Changed',
            'email' => 'random.dude@gmail.com',
            'userGroupId' => 1,
            'userStatusId' => 1,
            'password' => '',
            'password2' => ''
        );
        $expectedUser = $inputUser;

        $controller = self::initController('User',
                array('data' => json_encode($inputUser)));
        $controller->_update();
        unset($controller);

        $expected = '{"success":true,"message":"User Changed updated","data":['.
            json_encode($expectedUser).'],"total":1}';
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual, 'Unexpected output');

        $expectedName = array(array('username' => 'Changed'));
        $actualName = Lib_QueryWrapper::runQuery('SELECT username FROM User WHERE id = 2');
        $this->assertEquals($expectedName, $actualName);
    }

    public function test_update_password()
    {
        $inputUser = array(
            'id' => 2,
            'username' => 'Changed',
            'email' => 'random.dude@gmail.com',
            'userGroupId' => 1,
            'userStatusId' => 1,
            'password' => 'Uus1S4la5ana',
            'password2' => 'Uus1S4la5ana'
        );
        $expectedUser = array(
            'id' => 2,
            'username' => 'Changed',
            'email' => 'random.dude@gmail.com',
            'userGroupId' => 1,
            'userStatusId' => 1,
            'password' => '',
            'password2' => ''
        );;

        $controller = self::initController('User',
                array('data' => json_encode($inputUser)));
        $controller->_update();
        unset($controller);

        $expected = '{"success":true,"message":"User Changed updated","data":['.
            json_encode($expectedUser).'],"total":1}';
        $actual = self::getOutput();
        $this->assertEquals($expected, $actual, 'Unexpected output');

        $expectedPW = array(array('password' =>
            md5('ChangedUus1S4la5ana'.Model_User::PASS_SALT)));
        $actualPW = Lib_QueryWrapper::runQuery(
                'SELECT password FROM User WHERE id = 2');
        $this->assertEquals($expectedPW, $actualPW);
    }
}